# -*- conding:UTF-8 -*-

'''
@ Project: funny
@ File: music.py
@ Author: 韩成瑾
@ Date：2022/10/17 20:29
url = 'https://music.163.com/discover/toplist'

'''


import requests
import re

headers = {
    # 用户基本信息
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.47",
    # 浏览器基本信息
    "cookie":"JSESSIONID-WYYY=%2B5wzAe2Sa8F9SU3w5yU%2F2xOyhKZBbqupDc%2F39lBrP09p4HOws5WqiCOGzoCBHDxsHm9uNVcPkjG1pVY%5CtjUeKpKKXN4y4a3%5CbpOzJTa9mUeZTKeBW4Ng%5CO8srQJtyB0eB64q7cmVzljVJJc7Wd2pgTOmWPoJFlS9B3nxsBJecng5eGgZ%3A1666354082148; _iuqxldmzr_=32; _ntes_nnid=2beea89d3734dd94c15d77c150d83d75,1666352282197; _ntes_nuid=2beea89d3734dd94c15d77c150d83d75; NMTID=00O_NazznaHOvUIYEZuhKbjjl5K7iYAAAGD-lYB4A; WEVNSM=1.0.0; WNMCID=roxskd.1666352282469.01.0; WM_NI=siXofZXzceqwla7BFVfpiRS7qB0CWAiUoNsKL%2FWC6omoyMsG04vgCBrS6MUHQYyGzUO0D3uklN9vLTg%2FCOMSL0Hp%2Bcr9mRoqnOJ1neqiTS5uGxZAMAIoV2Yzul6JPM%2BCcUQ%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeb9e159f79fe58ee75c93b88aa7d15b929a8bacd549b3abadabec6192a988a4eb2af0fea7c3b92ab1b79890e5529ca6ad86f47db0bff9a3c55ea6b0a7b3d23db39e00a4d64bab8ba1aff24190f199a4b565afb1b7aef24df4ecba88c24ab28dfaaed8449387b7a8f95f8df08cd4f861b092f797cf60a5f18db0d05990f08891aa69b2e7a3b3ee7bae9186d2ed6fb3b79cb4b87f85a896a2d04b8bb19ad3dc72b7b4faabd347e99dadb6d437e2a3; WM_TID=NMBI11VEsUxAVEFUUBeVXr6NYmhBgDCg"
}

url = 'https://music.163.com/discover/toplist?id=3779629'

# 1.发送请求
reponse = requests.get(url=url,headers=headers)

# 2.获取数据
# print(reponse)  # <Response [200]>
html_data = reponse.text

# 3.解析数据 筛选想要的内容(歌曲名称  歌曲id)
# re：正则
# <li><a href="/song\?id=.*?">.*?</a></li>
info_list = re.findall('<li><a href="/song\?id=(.*?)">(.*?)</a></li>',html_data)  # (.*?) 括号:想要什么就括起来
# print(info_list)

# 4.保持数据
for info in info_list:
    music_url = 'https://music.163.com/#/discover/toplist?id=' + info[0]
    music_data = requests.get(music_url).content
    with open(f'music/{info[1]}.mp3',mode='wb')as f:
        f.write(music_data)















